<?php

/**
 * This is the model class for table "ticket".
 *
 * The followings are the available columns in table 'ticket':
 * @property integer $id
 * @property integer $userid
 * @property integer $eventid
 * @property string $code
 * @property string $createtime
 */
class Ticket extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Ticket the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'ticket';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('userid, eventid, code', 'required'),
			array('userid, eventid, winning, stat', 'numerical', 'integerOnly'=>true),
			array('code', 'length', 'max'=>12),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, userid, eventid, code, winning, stat, createtime, wintime', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'userid' => '用户', 
			'eventid' => '活动', 
			'code' => '奖券号码', 
			'winning' => '是否中奖', 
			'stat' => '已兑奖', 
			'createtime' => '参与时间',
			'wintime' => '中奖时间',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('userid',$this->userid);
		$criteria->compare('eventid',$this->eventid);
		$criteria->compare('code',$this->code,true);
		$criteria->compare('winning',$this->winning,true);
		$criteria->compare('stat',$this->stat,true);
		$criteria->compare('createtime',$this->createtime,true);

		$criteria->order=' id desc ';

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

	public function md5(){
		$key = $this->userid.$this->eventid.$this->code.strlen($this->code);
		return md5($key);
	}

	public function toCode(){
		$key = sprintf("%03d", $this->userid).' '.sprintf("%03d", $this->eventid).' '.$this->code;
		return $key;
	}




}